// pages/collecrecomend/collecrecomend.js
var app = getApp();
var lng;
var lat;
var dayTravelList_obj;//出行行程安排

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_index:1,

    side_index:1,
    selected_item:{},//被选中的行业

    x_longitude: '118.60',
    y_latitude: '24.92',

    industry: [],//行业
    
    //标记点用于在地图上显示标记的位置
    markers: [{
      iconPath: "../../images/position.png", //根据不同类型显示不同图片
      id: 0,
      latitude: 24.92,
      longitude: 118.60,
      width: 25,
      height: 32,
      //气泡
      callout: {
        color: '#3F3F3F',
        content: "景点名称",
        fontSize: 14,
        borderRadius: 5,
        bgColor: '#fff',
        padding: 5,
        textAlign: 'center',
        display: 'ALWAYS'
      }
    }],

    is_mapView:true,
    list:[{
      
    }]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // siteListByIndustryType
    var that = this;
    var obj = JSON.parse(options.obj);
    
    dayTravelList_obj = obj;
    console.log(obj);

    app.getLocation(function (result) {
      lng = result.longitude;
      lat = result.latitude;
    });

    that.industryTypeList(obj);

    // that.get_listData()
  },
  /**
   * 获取行业
   */
  industryTypeList:function(obj){
    var that = this;
    app.apiPost(app.apiList.industryTypeList, {
      content: obj
    }, function (res) {
      console.log(res, '获取行业'); 
      var item = res.content[that.data.side_index];

      that.setData({
        industry: res.content,
        selected_item: item
      })
     
      that.get_listData()
    })
  },
  /**
   *获取列表数据 
   */
  get_listData:function(){

    var that = this;
    app.apiPost(app.apiList.siteListByIndustryType, {
      content: {
        pageNum: that.data.page_index,
        pageSize:20,
        industryTypeId: that.data.selected_item.id,
        lng: lng,
        lat: lat
      }
    }, function (res) {
      console.log(res,'获取列表数据');
      that.setData({
        list: res.content.list
      })

    });
  },
  /**
   * 点击侧边
   */
  side_click:function(e){
    console.log(e.detail,'点击侧边');
    var that = this;
    if (this.data.side_index == e.detail) return;

    var item = that.data.industry[e.detail];
    this.setData({
      side_index: e.detail,
      selected_item: item
    })

    //刷新数据
    that.get_listData()
  },

  show: function () {
    console.log('地图显示');
  },
  /**
   * 点击气泡
   */
  markertap: function () {
    console.log('marks-气泡');

    //回到上一页
    wx.navigateBack({
      delta: 1
    });
  },
  regionchange: function () {
    console.log('拖动地图');
  },
  switch_view:function(){
    console.log('切换');

    var isMapView = !this.data.is_mapView;
    this.setData({
      is_mapView: isMapView
    })
  },
 /**
   * 添加
   */
  add:function(e){

    var that = this;
    console.log(e.currentTarget.dataset.item, dayTravelList_obj);

    var travel_obj = {
      id: dayTravelList_obj.id,
      name: dayTravelList_obj.trip_item.name,
      startDate: dayTravelList_obj.startDate,
      price: dayTravelList_obj.trip_item.price,
      validitySdate: dayTravelList_obj.trip_item.validitySdate,
      validityEdate: dayTravelList_obj.trip_item.validityEdate,
      travelType: dayTravelList_obj.trip_item.travelType,
      startAddress: dayTravelList_obj.startAddress,
      dayInfo: {}
    }

    var ary = dayTravelList_obj.dayListTravel;
    var item = ary[dayTravelList_obj.clickday_index];
    
    
    var obj = {
      siteId: e.currentTarget.dataset.item.id
    }
    item.daySiteList.push(obj);

    //去掉重复项
    item.daySiteList = that.deteleObject(item.daySiteList);

    // console.log(ary,'ary');

    travel_obj.dayInfo = item;
    
    console.log(travel_obj,'travel_obj');

    app.apiPost(app.apiList.updateUserTravel, {
      content: travel_obj
    }, function (res) {
      console.log(res, '新增用户行程');

      wx.navigateBack({
        delta: 1
      });
    })
    
  },
  /**
   * 删除重复项
   */
  deteleObject: function (obj) {
    var uniques = [];
    var stringify = {};
    for (var i = 0; i < obj.length; i++) {
      var keys = Object.keys(obj[i]);
      keys.sort(function (a, b) {
        return (Number(a) - Number(b));
      });
      var str = '';
      for (var j = 0; j < keys.length; j++) {
        str += JSON.stringify(keys[j]);
        str += JSON.stringify(obj[i][keys[j]]);
      }
      if (!stringify.hasOwnProperty(str)) {
        uniques.push(obj[i]);
        stringify[str] = true;
      }
    }
    uniques = uniques;
    return uniques;

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

/*
*

*/
